﻿using UnityEngine;
using System.Collections;

public class CorePlace : MonoBehaviour {

    public Vector3 worldPos;
    public float gridSize = 4;
    public bool alwaysShowGrid = false;
    public string parentName = "";


#if UNITY_EDITOR

    void OnDrawGizmos()
    {

        if (!alwaysShowGrid && UnityEditor.Selection.activeGameObject != this.gameObject) return;
        //draw grid
        Gizmos.color = Color.red;

        Vector3 upperLeft = Camera.current.ViewportToWorldPoint(new Vector3(0, 0, 0));
        Vector3 lowerRight = Camera.current.ViewportToWorldPoint(new Vector3(1, 1, 0));

        float curWidth = lowerRight.x - upperLeft.x;

        //overdraw by two for improved looks
        int lineCount = (int)(curWidth / gridSize) + 2;

        Vector3 startPosition = Camera.current.transform.position;
        startPosition.x -= (lineCount / 2) * gridSize;
        //keep on grid
        startPosition.x -= startPosition.x % gridSize;

        //draw verticle lines
        for (int i = 0; i < lineCount; i++)
        {
            Gizmos.DrawLine(new Vector3(startPosition.x, -1000000, 0), new Vector3(startPosition.x, 1000000, 0));
            startPosition.x += gridSize;
        }

        float curHeight = lowerRight.y - upperLeft.y;
        //overdraw by two for improved looks
        lineCount = (int)(curHeight / gridSize) + 2;

        startPosition = Camera.current.transform.position;
        startPosition.y += (lineCount / 2) * gridSize;
        //keep on grid
        startPosition.y += (gridSize - startPosition.y % gridSize);

        //draw horizontal lines
        for (int i = 0; i < lineCount; i++)
        {
            Gizmos.DrawLine(new Vector3(-1000000, startPosition.y, 0), new Vector3(1000000, startPosition.y, 0));
            startPosition.y -= gridSize;
        }

        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(worldPos - (new Vector3(0, 0, -1)), new Vector3(gridSize, gridSize, gridSize));
    }

#endif
}
